package com.lbssoft.gpsbd.dao;

import com.lbssoft.gpsbd.model.Group;
import com.lbssoft.gpsbd.model.Log;
import com.lbssoft.gpsbd.model.LoginLog;
import com.lbssoft.gpsbd.model.vo.Move;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GroupMapper {

	/**
	 * 根据用户id获取分组数据
	 * @param userId
	 * @return
     */
	@Select("SELECT a.id,a.name,IFNULL(a.groupId,0) as groupId FROM lbs_user_group t LEFT JOIN lbs_groups a ON a.id = t.groupId WHERE t.userId = #{userId}")
	List<Group> list(@Param("userId") long userId);


	/**
	 * 添加分组
	 * @param group
     */
	@Insert("INSERT INTO lbs_groups(id,name,groupId,uuid) VALUE(#{id},#{name},#{groupId},#{uuid})")
	@Options(useGeneratedKeys=true, keyProperty="id")
	void add(Group group);

	/**
	 * 写入分组和用户的关系
	 * @param userId
	 * @param groupId
     */
	@Insert("INSERT INTO lbs_user_group(userId,groupId) VALUE(#{userId},#{groupId})")
	void addUserGroup(@Param("userId") long userId,@Param("groupId") int groupId);

	/**
	 * 删除分组
	 * @param id
     */
	@Delete("DELETE FROM lbs_groups where id  = #{id}")
	void delete(@Param("id") int id);

	/**
	 * 更新分组信息
	 * @param group
     */
	@Update("UPDATE lbs_groups SET name = #{name},groupId=#{groupId} where id = #{id}")
	void update(Group group);

	/**
	 * 移动分组
	 * @param move
     */
	@Update("UPDATE lbs_groups SET groupId=#{groupId} where id = #{id}")
	void move(Move move);

	/**
	 * 根据id获取分组信息
	 * @param id
	 * @return
     */
	@Select("SELECT * FROM lbs_groups where id = #{id}")
	Group getGroupById(@Param("id") int id);
}
